import {Component, OnInit} from "@angular/core";
import {HttpClient} from "@angular/common/http";
import {LocalDataSource} from "ng2-smart-table";
import {map} from "rxjs/operators";
import {BrwxdwxtTableRenderComponent} from "./brwxdwxt-table-render.component";
import {BrwxdwxtZoneTableRenderComponent} from "./brwxdwxt-zone-table-render.component";

@Component({
  templateUrl:'./brwxdxt.component.html',
  styles:[`
    .input-rows{
        margin-bottom: 10px;
    }
    .input-rows input{
        width: 13%;
        vertical-align: middle;
        margin-right: 10px;
    }
    .input-rows nb-select{
        vertical-align: middle;
        margin-right: 10px;
    }
    .input-rows button{
        vertical-align: middle;
    }
  `]
})
export class BrwxdxtComponent implements OnInit {
  settings = {
    pager: {
      display: true,
      perPage: 50
    },
    actions: {
      add: false,
      edit: false,
      delete: false,
    },
    hideSubHeader: true,
    columns: {
      name: {
        title: '病人姓名',
        type: 'string',
      },
      ringId: {
        title: '手环编号',
        type: 'string',
      },
      sex:{
        title: '性别',
        type: 'string'
      },
      age:{
        title: '出生年份',
        type: 'number'
      },
      zone:{
        title: '病区',
        type: 'string'
      },
      contactPerson: {
        title: '负责人',
        type: 'string',
      },
      contactPhone: {
        title: '联系电话',
        type: 'string'
      },
      action:{
        title:'操作',
        type: 'custom',
        renderComponent: BrwxdwxtTableRenderComponent
      }
    },
  };
  zone_settings = {
    pager: {
      display: true,
      perPage: 20
    },
    actions: {
      add: false,
      edit: false,
      delete: false,
    },
    hideSubHeader: true,
    columns:{
      name:{
        title:'病区',
        type:'string'
      },
      contactPerson:{
        title: '联系人',
        type:'string'
      },
      contactPhone:{
        title: '联系电话',
        type: 'string'
      },
      action:{
        title:'操作',
        type:'custom',
        renderComponent: BrwxdwxtZoneTableRenderComponent
      }
    }
  };
  source:MyDataSource;
  zone_source:MyZoneDataSource;
  zone_edit:{name:string,contactPerson:string,contactPhone:string} = {name:'',contactPerson:'',contactPhone:''};
  edit_patientName:string = '';
  edit_sex:string = '';
  edit_age:number;
  edit_zone:string = '';
  edit_cardId:string = '';
  edit_contactPerson:string ='';
  edit_contactPhone: string = '';
  error = '';
  constructor(private httpClient: HttpClient) {
    this.source = new MyDataSource(httpClient);
    this.zone_source = new MyZoneDataSource(httpClient);
  }

  ngOnInit(): void {

  }

  selectZone(){
    const where = "o.name='"+this.edit_zone+"'";
    this.httpClient.get('http://122.224.253.202:20020/x_query_assemble_surface/jaxrs/table' +
      '/list/b69884d9-3d4e-41a7-a64b-520fee5b3a8b' +
      '/row/select' +
      '/offset/0' +
      '/count/1' +
      '/where/'+(where),{withCredentials:true}).subscribe(res=>{
        if(res['data'].length>0){
          this.edit_contactPerson = res['data'][0]['contactPerson'];
          this.edit_contactPhone = res['data'][0]['contactPhone'];
        }
    })
  }

  add(){
    if(this.edit_cardId.length==0 || this.edit_patientName.length==0 || this.edit_contactPerson.length==0){
      this.error = '信息不完整';
      return;
    }
    this.httpClient.post('http://122.224.253.202:20020/x_query_assemble_surface/jaxrs/table/478efbcf-2cd6-4d42-ae60-69938246861f/row',{
      "ringId":this.edit_cardId,
      "name":this.edit_patientName,
      "age":this.edit_age,
      "sex":this.edit_sex,
      "zone":this.edit_zone,
      "contactPerson":this.edit_contactPerson,
      "contactPhone":this.edit_contactPhone
    },{
      withCredentials: true
    }).subscribe(res=>{
      if(res['data']['value']===true){
        this.edit_patientName = '';
        this.edit_cardId = '';
        this.edit_age = null;
        this.edit_sex = '';
        this.edit_zone = '';
        this.edit_contactPerson = '';
        this.edit_contactPhone = '';
        this.source.refresh();
      }else{
        this.error = '添加病人失败';
      }
    })
  }

  addZone(){
    if(this.zone_edit.name.length==0 || this.zone_edit.contactPerson.length == 0 || this.zone_edit.contactPhone.length == 0){
      this.error = '信息不完整';
      return;
    }
    this.httpClient.post('http://122.224.253.202:20020/x_query_assemble_surface/jaxrs/table/b69884d9-3d4e-41a7-a64b-520fee5b3a8b/row'
      ,this.zone_edit
      ,{withCredentials: true}).subscribe(res=>{
      if(res['data']['value']===true){
        this.zone_edit.name = '';
        this.zone_edit.contactPhone = '';
        this.zone_edit.contactPerson = '';
        this.zone_source.refresh();
      }else{
        this.error = '添加病区失败';
      }
    });
  }
}

export class MyDataSource extends LocalDataSource {
  all: number;
  constructor(private httpClient:HttpClient) {
    super();
    this.pagingConf['page'] = 1;
  }
  count(): number {
    return this.all;
  }
  getElements(): Promise<any> {
    const page = this.pagingConf['page'];
    const perPage = Number(this.pagingConf['perPage']);
    return this.httpClient.get( 'http://122.224.253.202:20020/x_query_assemble_surface/jaxrs/table'
      +'/list/478efbcf-2cd6-4d42-ae60-69938246861f'
      +'/row/0'
      +'/offset/'+(page-1)*perPage
      +'/next/'+perPage,{
      withCredentials:true
    }).pipe(map(res=>{
      this.all = res['count'];
      res['data'].forEach(data=>{
        data['self'] = this;
      });
      return res['data'];
    })).toPromise();
  }
  delete(id):Promise<boolean>{
    return this.httpClient.delete('http://122.224.253.202:20020/x_query_assemble_surface/jaxrs/table/478efbcf-2cd6-4d42-ae60-69938246861f/row/' + id, {
      withCredentials: true
    }).pipe(map(res=>{
        if (res['data']['value'] === true) {
          this.refresh();
          return true
        }else{
          return false;
        }
    })).toPromise();
  }

  edit(id,data):Promise<boolean>{
    return this.httpClient.put('http://122.224.253.202:20020/x_query_assemble_surface/jaxrs/table/478efbcf-2cd6-4d42-ae60-69938246861f/row/'+id,{
      'name':data['name'],
      'ringId':data['ringId'],
      'sex':data['sex'],
      'age':data['age'],
      'zone':data['zone'],
      'contactPerson':data['contactPerson'],
      'contactPhone':data['contactPhone']
    },{
      withCredentials: true
    }).pipe(map(res=>{
      if (res['data']['value'] === true) {
        this.refresh();
        return true
      }else{
        return false;
      }
    })).toPromise();
  }
}

export class MyZoneDataSource extends LocalDataSource {
  all: number;
  data:{name:string,contactPerson:string,contactPhone:string}[] = [];
  constructor(private httpClient:HttpClient) {
    super();
    this.pagingConf['page'] = 1;
  }
  count(): number {
    return this.all;
  }
  getElements(): Promise<any> {
    const page = this.pagingConf['page'];
    const perPage = Number(this.pagingConf['perPage']);
    console.log((page-1)*perPage);
    return this.httpClient.get( 'http://122.224.253.202:20020/x_query_assemble_surface/jaxrs/table'
      +'/list/b69884d9-3d4e-41a7-a64b-520fee5b3a8b'
      +'/row/0'
      +'/offset/'+(page-1)*perPage
      +'/next/'+perPage,{
      withCredentials:true
    }).pipe(map(res=>{
      this.all = res['count'];
      this.data = res['data'];
      res['data'].forEach(data=>{
        data['self'] = this;
      });
      return res['data'];
    })).toPromise();
  }
  delete(id):Promise<boolean> {
    return this.httpClient.delete('http://122.224.253.202:20020/x_query_assemble_surface/jaxrs/table/b69884d9-3d4e-41a7-a64b-520fee5b3a8b/row/' + id, {
      withCredentials: true
    }).pipe(map(res => {
      if (res['data']['value'] === true) {
        this.refresh();
        return true
      } else {
        return false;
      }
    })).toPromise();
  }
}
